package com.tony.demo.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.puhui.lib.utils.DensityUtil;
import com.tony.demo.R;
import com.tony.demo.mvp.presenter.impl.BasePresenter;
import com.tony.demo.ui.base.BaseActivity;

import java.util.List;

/**
 * Copyright:
 * Created by TangJian on 2019/4/19.
 * Description:
 * Modified:
 */

public class SplashActivity extends BaseActivity {
    private ImageView splashImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 点击安装包进行安装，安装结束后不点击完成，而是点击打开应用，应用启动后，再回到桌面，
        // 从桌面点击应用图标会造成反复重启应用的现象。
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            this.finish();
            return;
        }
        // 防止第三方跳转时出现双实例
        List<Activity> activityList = ActivityUtils.getActivityList();
        for (Activity activity : activityList) {
            if (activity instanceof MainActivity) {
                if (!activity.isFinishing()) {
                    finish();
                    return;
                }
            }
        }

        redirectTo();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getViewLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void setStatusBar() {
    }

    // 渐变展示启动屏
    private void startAnimate(View view) {
        AnimationSet as = new AnimationSet(true);

        AlphaAnimation aa = new AlphaAnimation(0.7f, 1.0f);
        aa.setDuration(1500);

        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) splashImage.getLayoutParams();
        int pivotY = (DensityUtil.getScreenHeight(this)
                - layoutParams.bottomMargin - splashImage.getPaddingBottom()) / 2;
        ScaleAnimation sa = new ScaleAnimation(
                0.7f, 1.0f, 0.7f, 1.0f, DensityUtil.getScreenWidth(this) / 2f, pivotY);
        sa.setDuration(0);

        as.addAnimation(aa);
        as.addAnimation(sa);
        as.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                redirectTo();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }
        });

        view.startAnimation(as);
    }

    /**
     * 跳转页面
     */
    protected void redirectTo() {
        //跳转到首页
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
